package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_city;
import cn.com.api.service.ApiT_cityService;
import cn.com.backstage.controller.base.T_cityBaseController;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_cityController extends T_cityBaseController {

	private static Logger logger = Logger.getLogger(T_cityController.class);

	@Autowired
	private ApiT_cityService apiT_cityService;

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_city_shop-add")
	public void t_city_shop_add(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String name = request.getParameter("name");
		String uname = request.getParameter("uname");
		String is_effect = request.getParameter("is_effect");
		String is_delete = request.getParameter("is_delete");
		String pid = request.getParameter("pid");
		String is_open = request.getParameter("is_open");
		String is_default = request.getParameter("is_default");
		String description = request.getParameter("description");
		String notice = request.getParameter("notice");
		String seo_title = request.getParameter("seo_title");
		String seo_keyword = request.getParameter("seo_keyword");
		String seo_description = request.getParameter("seo_description");
		String sort = request.getParameter("sort");
		String u_name = request.getParameter("u_name");
		String is_hot = request.getParameter("is_hot");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String send_distance = request.getParameter("send_distance");
		String open_time = request.getParameter("open_time");
		String address = request.getParameter("address");
		String contacts = request.getParameter("contacts");
		String tel = request.getParameter("tel");
		String is_auto_allocateorder = request.getParameter("is_auto_allocateorder");
		String same_distance = request.getParameter("same_distance");
		String send_fouraward = request.getParameter("send_fouraward");
		String send_fiveaward = request.getParameter("send_fiveaward");
		String return_goods_time = request.getParameter("return_goods_time");
		String sendlimit = request.getParameter("sendlimit");
		String sendfee = request.getParameter("sendfee");
		String reserve_money = request.getParameter("reserve_money");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		ApiT_city cur_city = apiT_cityService.findT_cityById(Integer.valueOf(id));
		// add
		if (cur_city == null) {
			ApiT_city t_city = new ApiT_city();

			// t_city.setId(id);
			t_city.setName(name);
			t_city.setUname(uname);
			t_city.setIs_effect(1);
			t_city.setIs_delete(0);
			// t_city.setPid(pid);
			t_city.setIs_open(1);
			t_city.setIs_default(0);
			t_city.setDescription(description);
			t_city.setNotice(notice);
			t_city.setSort(0);
			t_city.setU_name(u_name);
			t_city.setIs_hot(0);
			t_city.setLongitude(Float.valueOf(longitude));
			t_city.setLatitude(Float.valueOf(latitude));
			t_city.setSend_distance(Float.valueOf(send_distance));
			t_city.setOpen_time(open_time);
			t_city.setAddress(address);
			t_city.setContacts(contacts);
			t_city.setTel(tel);
			t_city.setIs_auto_allocateorder(Integer.valueOf(is_auto_allocateorder));
			t_city.setSame_distance(Double.valueOf(same_distance));
			t_city.setSend_fouraward(Double.valueOf(send_fouraward));
			t_city.setSend_fiveaward(Double.valueOf(send_fiveaward));
			t_city.setReturn_goods_time(Integer.valueOf(return_goods_time));
			t_city.setSendlimit(Util.getBigDecimal(sendlimit));
			t_city.setSendfee(Util.getBigDecimal(sendfee));
			t_city.setReserve_money(Double.parseDouble(reserve_money));
			apiT_cityService.insertT_cityAndShop(t_city);
		} else {// edit
			ApiT_city t_city = apiT_cityService.findT_cityById(Integer.valueOf(id));
			// t_city.setId(id);
			t_city.setName(name);
			t_city.setUname(uname);
			t_city.setIs_effect(1);
			t_city.setIs_delete(0);
			// t_city.setPid(pid);
			t_city.setIs_open(Integer.valueOf(is_open));
			t_city.setIs_default(0);
			t_city.setDescription(description);
			t_city.setNotice(notice);
			t_city.setSort(0);
			t_city.setU_name(u_name);
			t_city.setIs_hot(0);
			t_city.setLongitude(Float.valueOf(longitude));
			t_city.setLatitude(Float.valueOf(latitude));
			t_city.setSend_distance(Float.valueOf(send_distance));
			t_city.setOpen_time(open_time);
			t_city.setAddress(address);
			t_city.setContacts(contacts);
			t_city.setTel(tel);
			t_city.setIs_auto_allocateorder(Integer.valueOf(is_auto_allocateorder));
			t_city.setSame_distance(Double.valueOf(same_distance));
			t_city.setSend_fouraward(Double.valueOf(send_fouraward));
			t_city.setSend_fiveaward(Double.valueOf(send_fiveaward));
			t_city.setReturn_goods_time(Integer.valueOf(return_goods_time));
			t_city.setSendlimit(Util.getBigDecimal(sendlimit));
			t_city.setSendfee(Util.getBigDecimal(sendfee));
			t_city.setReserve_money(Double.parseDouble(reserve_money));

			apiT_cityService.updateT_cityAndShop(t_city);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
